﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

using UncleBobWebService.Models;
using UncleBobWebService.ServiceRequester;


namespace UncleBobWebService.Database
{
    class ApplicationDBAdapter : IDBAdapter
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="applicant"></param>
        /// <returns></returns>
        public override Message CreateData(Message msg, Object applicant)
        {
            throw new NotImplementedException();
        }


        public override Message SearchForAllData(Message msg)
        {
            JobApplication jobApp = (JobApplication)msg.Data;
            using (SqlConnection connection = new SqlConnection(mConnectionString))
                //using (SqlCommand command = connection.CreateCommand())

                mTable = new DataTable();
            mCommand = mConnection.CreateCommand();
            mCommand.CommandText = string.Format(
                "SELECT * FROM User_Job_Questions WHERE JobApplicationID = {0}", jobApp.AppID
                );


            SqlDataAdapter mAdapter = new SqlDataAdapter(mCommand);

            HMRegisterModel hmModel = new HMRegisterModel();
            //HMRegisterModel hmTempModel = new HMRegisterModel();

            try
            {
                mConnection.Open();
                mAdapter.Fill(mTable);
                mConnection.Close();

                hmModel.questions = new List<UserJobQuestions>();




                if (mTable.Rows.Count != 0)
                {
                    int mCount = mTable.Rows.Count;

                    do
                    {
                        msg.Result = true;
                        Message message = new Message();


                        UserJobQuestions questions = new UserJobQuestions();

                        questions.Question = mTable.Rows[mCount - 1]["Question"].ToString();
                        questions.Answer = mTable.Rows[mCount - 1]["Answer"].ToString();


                        hmModel.questions.Add(questions);


                        mCount--;
                    } while (mCount > 0);


                }

            }
            catch (Exception error)
            {
                msg.Result = false;
                msg.Data = new String(error.Message.ToCharArray());
            }

            msg = new Message(hmModel);
            return msg;



        }


        public override Message SearchForSpecificData(Message msg, Object user)
        {
            mTable = new DataTable();
            mCommand = mConnection.CreateCommand();
            mCommand.CommandText = string.Format("SELECT * FROM UserRegistration WHERE UserName = '{0}' AND Password = '{1}'"
                    , ((RegisterModel)user).Username
                    , ((RegisterModel)user).Password
            );
            SqlDataAdapter mAdapter = new SqlDataAdapter(mCommand);

            try
            {
                mConnection.Open();
                mAdapter.Fill(mTable);
                mConnection.Close();

                if (mTable.Rows.Count != 0)
                {
                    msg.Result = true;
                    ((RegisterModel)msg.Data).Username = mTable.Rows[0]["UserName"].ToString();
                    ((RegisterModel)msg.Data).FirstName = mTable.Rows[0]["FirstName"].ToString();
                    ((RegisterModel)msg.Data).LastName = mTable.Rows[0]["LastName"].ToString();
                }
                else
                {
                    msg.Result = false;
                    msg.Data = new String(string.Format("Account does not exist.\nUsername/Password miss match").ToCharArray());
                }
            }
            catch (Exception error)
            {
                msg.Result = false;
                msg.Data = new String(error.Message.ToCharArray());
            }

            return msg;
        }




        public override Message EditSpecificData(Message msg, Object data)
        {
            throw new NotImplementedException();
        }


        public override Message DeleteSpecificData(Message msg, Object data)
        {
            throw new NotImplementedException();
        }


        public override Message DeleteAllData(Message msg)
        {
            throw new NotImplementedException();
        }
    }
}
